package org.weed.remote.network.server;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

import org.weed.remote.network.model.HttpRequest;

public class HttpServer extends StreamServer {	
	
	public HttpServer(int port) {
		super(port);
	}
	
	@Override
	protected void connectionHandler(Socket client) {
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
			BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
			boolean isDone = false;
			while (!isDone) {
				HttpRequest request = HttpRequest.getRequest(reader);
				isDone = processRequest(request);
				String page = "<body>ttt</body>";
				byte[] content = page.getBytes();
				writer.write("HTTP/1.1 200 OK\n");
				writer.write("Content-Type: text/html\n");
				writer.write("Content-Length: "+content.length+"\n");
				writer.write("\n");
				writer.write(page);
				writer.flush();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private boolean processRequest(HttpRequest request) {
		System.out.println(request.getHeader());
		
		return false;
	}
	

	public static void main(String[] args) {
		HttpServer server = new HttpServer(80);
		server.run();
	}
}
